import logo from '@/assets/images/logo@2x.png'
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import { Button, Form, Input, App } from 'antd'
import { loginAPI } from '@/apis'
import "./index.scss"
import { useNavigate } from 'react-router-dom'
export default function Login() {
    const {message} = App.useApp()
    const navigate = useNavigate()
    const onFinish = (value) => {
        loginAPI(value).then(res => {
            message.open({
                type: res.code === 200 ? 'success' : 'error',
                content: res.message
            })
            if (res.code === 200) {
                navigate('/')
                localStorage.setItem('ksuser', value.username)
                localStorage.setItem('kspwd', value.password)
                localStorage.setItem('kshxtoken', res.data)
                
                //company_type是空，则对应的查询权限为ALL
                if( value.username != 'gspuser' ){
                    localStorage.setItem('company_type', '')
                }
            }
        })
    }
    return (
        <div className="login-container">
            <div className='login-content'>
                <div className='logo'>
                    <img src={logo} alt="" />
                    <h1>企业信用画像</h1>
                </div>
                <Form
                    name="login"
                    initialValues={{
                        remember: true,
                    }}
                    style={{
                        width: 360,
                    }}
                    onFinish={onFinish}
                >
                    <Form.Item
                        name="username"
                        rules={[
                            {
                                required: true,
                                message: '请输入账号',
                            },
                        ]}
                    >
                        <Input prefix={<UserOutlined />} size="large" placeholder="请输入用户名" allowClear />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[
                            {
                                required: true,
                                message: '请输入密码',
                            },
                        ]}
                    >
                        <Input.Password prefix={<LockOutlined />} size="large" placeholder="请输入密码" />
                    </Form.Item>
                    <Form.Item>
                        <Button block type="primary" size="large" htmlType="submit">
                            登 录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}